package com.example.Servlet;

import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.HttpCookie;
import java.util.Enumeration;

public class Logout extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();


        Cookie[] cookies = req.getCookies();
        //System.out.println("有多少个cookie:"+cookies.length);
        for(Cookie c:cookies){
            //System.out.println(c.getName()+" "+c.getValue());
            c.setMaxAge(0); //设置cookie失效
        }

        //1.将session中的所有键值属性移除，即销毁
        /*Enumeration enumeration = req.getAttributeNames();
        while (enumeration.hasMoreElements()){
            session.removeAttribute(enumeration.nextElement().toString());
        }*/

        HttpSession session = req.getSession();
        session.invalidate();       //销毁用户跟session的关联
        //session.setMaxInactiveInterval(0);  //设置session会话立即失效
        System.out.println("退出成功！");
        resp.sendRedirect("/Barbr/html/login.jsp");
    }
}
